package com.itany.corejava.code09_集合.map集合;

import java.util.*;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年11月21日 17:23
 */
public class Test05_练习 {
    public static void main(String[] args) {
        // System.out.println(test01());

        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一串字符串:");
        String str = sc.next();
        // System.out.println(test02(str));
        System.out.println(test03(str));

    }

    public static HashMap<Integer, Integer> test01() {
        // 有一个ArrayList,里面的值是50-60
        List<Integer> list = new ArrayList<>();
        for (int i = 50; i <= 60; i++) {
            list.add(i);
        }
        System.out.println(list);


        // 将该ArrayList的值存入到HashMap中去，索引作为key，值作为value
        // 输出HashMap集合
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(i, list.get(i));
        }
        return map;
    }

    //提示用户输入一个字符串，统计每个字符出现的次数，保存到HashMap中去
    // abcaab


    public static HashMap<Character, Integer> test02(String s) {//abbaad
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);//a

            //判断当前集合中是否有key为c的
            if (map.containsKey(c)) {
                int count = map.get(c);//根据当前的key来获取对应的value值
                count++;
                map.put(c, count);//重新覆盖之前的值，修改之前的value

            } else {
                map.put(c, 1);//{'a'=1,'b'=1}
            }
        }
        return map;

    }

    public static HashMap<Character, Integer> test03(String s) {//abbaad
        HashMap<Character, Integer> map = new HashMap<>();
        while (!s.isEmpty()) {
            char c = s.charAt(0);//a   b  d
            String s2 = s.replace(c + "", "");//bbd    d    ""
            map.put(c, s.length() - s2.length());//{'a'=3,'b'=2,'d'=1}
            s = s2;//s="bbd"   s="d"  s=""

        }

        return map;

    }


}
